package com.cv.infra.controller.system.dynamicThreadPool.vo;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 基础设施 - 动态线程池基本配置信息 Response VO
 *
 * @author Charles_XDXD
 */
@Data
@Schema(description = "基础设施 - 动态线程池基本配置信息 Response VO")
public class DynamicThreadPoolRespVO {

    /**
     * 核心线程数
     */
    @Schema(description = "核心线程数", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
    private Integer corePoolSize;

    /**
     * 最大线程数
     */
    @Schema(description = "最大线程数", requiredMode = Schema.RequiredMode.REQUIRED, example = "20")
    private Integer maximumPoolSize;

    /**
     * 队列最大容量
     */
    @Schema(description = "队列最大容量", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
    private Integer queueCapacity;

    /**
     * 保持活跃时间
     */
    @Schema(description = "保持活跃时间", requiredMode = Schema.RequiredMode.REQUIRED, example = "60")
    private Long keepAliveTime;

    /**
     * 当前活动的线程数
     */
    @Schema(description = "当前活动的线程数", requiredMode = Schema.RequiredMode.REQUIRED, example = "15")
    private Integer activeThreads;

    /**
     * 已完成的任务数
     */
    @Schema(description = "已完成的任务数", requiredMode = Schema.RequiredMode.REQUIRED, example = "30")
    private Long completedTasks;

    /**
     * 队列中的任务数
     */
    @Schema(description = "队列中的任务数", requiredMode = Schema.RequiredMode.REQUIRED, example = "5")
    private Integer queueSize;

    /**
     * 操作系统名称
     */
    @Schema(description = "操作系统名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "Linux")
    private String osName;

    /**
     * 操作系统版本
     */
    @Schema(description = "操作系统版本", requiredMode = Schema.RequiredMode.REQUIRED, example = "5.15.49-1-MANJARO")
    private String osVersion;

    /**
     * 操作系统架构
     */
    @Schema(description = "操作系统架构", requiredMode = Schema.RequiredMode.REQUIRED, example = "amd64")
    private String osArch;

    /**
     * 可用处理器数量
     */
    @Schema(description = "可用处理器数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "8")
    private Integer availableProcessors;

    /**
     * 总内存
     */
    @Schema(description = "总内存", requiredMode = Schema.RequiredMode.REQUIRED, example = "8589934592")
    private Long totalMemory;

    /**
     * 已用内存
     */
    @Schema(description = "已用内存", requiredMode = Schema.RequiredMode.REQUIRED, example = "8589934592")
    private Long usedMemory;

    /**
     * 最大内存
     */
    @Schema(description = "最大内存", requiredMode = Schema.RequiredMode.REQUIRED, example = "8589934592")
    private Long maxMemory;

    /**
     * 初始化线程池和系统信息
     *
     * @param executor 线程池
     */
    public DynamicThreadPoolRespVO(ThreadPoolExecutor executor) {
        // 初始化线程池信息
        this.corePoolSize = executor.getCorePoolSize();
        this.maximumPoolSize = executor.getMaximumPoolSize();
        BlockingQueue<?> queue = executor.getQueue();
        this.queueCapacity = queue.remainingCapacity() + queue.size();
        this.keepAliveTime = executor.getKeepAliveTime(TimeUnit.SECONDS);
        this.activeThreads = executor.getActiveCount();
        this.completedTasks = executor.getCompletedTaskCount();
        this.queueSize = queue.size();

        // 初始化系统信息
        this.osName = System.getProperty("os.name");
        this.osVersion = System.getProperty("os.version");
        this.osArch = System.getProperty("os.arch");
        this.availableProcessors = Runtime.getRuntime().availableProcessors();
        this.totalMemory = Runtime.getRuntime().totalMemory();
        this.usedMemory = this.totalMemory - Runtime.getRuntime().freeMemory();
        this.maxMemory = Runtime.getRuntime().maxMemory();
    }
    
}